var newVue = new Vue({
	el: "body",
	data: {
		products: [],
		limit: [],
		buyone: [],
		limitSecond: "",
		limitMinute: "",
		limitHour: "",
		limitDay: "",
		limitInfo: "",
		isActivity: "",
		isStart:true
	},
	created: function() {
		init();
		_this = this
		loadProduct(_this);
		loadBuyOne(_this);
		loadLimit(_this);
	},
	methods: {
		detail: function(id) {
			location.href = "../detail/detail.html?id=" + id
		},
		cart: function(a) {
			event.stopPropagation();
			indexAddCart(a)
		},
		cartLimit: function(a) {
			event.stopPropagation();
			limitAddCart(a)
		}
	}
})

function setTime(obj) {
	var a = setInterval(function() {
		var etime = new Date(obj.limit[0].attributes.endTime).getTime()
		var stime = new Date(obj.limit[0].attributes.startTime).getTime()
		var ntime = new Date().getTime()
		if(ntime - stime < 0) {
			obj.limitInfo = "活动尚未开始，请耐心等待"
			var leftTime = stime - ntime;
			var dd = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
			var hh = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时数
			var mm = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟数
			var ss = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数
			obj.limitSecond = add0(ss);
			obj.limitMinute = add0(mm);
			obj.limitHour = add0(hh);
			obj.limitDay = add0(dd);
			obj.isActivity = "距开始"
		} else {
			if(ntime > etime) {
				obj.limitInfo = "活动已经结束，尽请期待下轮"
				obj.limitSecond = 00;
				obj.limitMinute = 00;
				obj.limitHour = 00;
				obj.limitDay = 00;
				obj.isActivity = "已结束"
			} else {
				obj.limitInfo = "活动已经开始，请尽快下单"
				obj.isStart=false
				var leftTime = etime - ntime;
				var dd = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
				var hh = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时数
				var mm = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟数
				var ss = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数
				obj.limitSecond = add0(ss);
				obj.limitMinute = add0(mm);
				obj.limitHour = add0(hh);
				obj.limitDay = add0(dd);
				obj.isActivity = "距结束"
			}
		}
	}, 1000)
}

function add0(n) {
	var _n = parseInt(n, 10); //解析字符串,返回整数
	if(_n > 0) {
		if(_n <= 9) {
			_n = "0" + _n
		}
		return String(_n);
	} else {
		return "00";
	}
}

function loadProduct(a) {
	var query = new AV.Query('Product');
	query.equalTo("activity", "none");
	query.find().then(function(products) {
		a.products = products
	}).catch(function(error) {
		console.log(JSON.stringify(error));
	});
}

function loadBuyOne(a) {
	var activity2 = new AV.Query('Product');
	activity2.equalTo("activity", "buyone");
	var isActivity2 = new AV.Query('Product');
	isActivity2.equalTo("isActivity", "true");
	var query2 = AV.Query.and(activity2, isActivity2);
	query2.find().then(function(products) {
		a.buyone = products
	}).catch(function(error) {
		console.log(JSON.stringify(error));
	});
}

function loadLimit(a) {
	var activity1 = new AV.Query('Product');
	activity1.equalTo("activity", "limit");
	var isActivity1 = new AV.Query('Product');
	isActivity1.equalTo("isActivity", "true");
	var query2 = AV.Query.and(activity1, isActivity1);
	query2.find().then(function(products) {
		a.limit = products;
		setTime(a)
	}).catch(function(error) {
		console.log(JSON.stringify(error));
	});
}

function addCart(a, b) {
	var Product = AV.Object.extend('Cart')
	var product = new Product();
	product.set('username', a);
	product.set('pid', b);
	product.set('num', 1);
	product.save().then(function() {
		alert("success")
	}, function(error) {
		alert("failure")
	});
}

function indexAddCart(t) {
	currentUser = AV.User.current();
	if(currentUser) {
		var pid = _this.products[t].attributes.pid //商品id
		var user = currentUser.attributes.username //当前登录用户
		var id = new AV.Query('Cart');
		id.equalTo("pid", pid);
		var username = new AV.Query('Cart');
		username.equalTo("username", user);
		//设置组合查询的条件
		var cart = AV.Query.and(id, username);
		cart.find().then(function(products) {
			if(products.length == 0) {
				addCart(user, pid)
			} else {
				var query = new AV.Query('Cart');
				query.equalTo("pid", pid);
				query.find().then(function(products) {
					console.log(products)
					var num = products[0].attributes.num //当前购物车里该商品的数量  
					var rid = products[0].id //该商品在购物车里的真实id
					console.log(rid)
					console.log(num)
					//购物车里已经添加过当前商品，只是修改数量
					var todo = AV.Object.createWithoutData('Cart', rid);
					todo.set('num', ++num);
					// 保存到云端
					todo.save();
					alert("success")
				}).catch(function(error) {
					console.log(JSON.stringify(error));
				});
			}
		}).catch(function(error) {
			console.log(JSON.stringify(error));
		});
	} else {
		location.href = "../login/login.html?RetUrl=" + getPageUrl()
	}
}

function limitAddCart(t) {
	currentUser = AV.User.current();
	if(currentUser) {
		var pid = _this.limit[t].attributes.pid //商品id
		var user = currentUser.attributes.username //当前登录用户
		var id = new AV.Query('Cart');
		id.equalTo("pid", pid);
		var username = new AV.Query('Cart');
		username.equalTo("username", user);
		//设置组合查询的条件
		var cart = AV.Query.and(id, username);
		cart.find().then(function(products) {
			if(products.length == 0) {
				addCart(user, pid)
			} else {
				var query = new AV.Query('Cart');
				query.equalTo("pid", pid);
				query.find().then(function(products) {
					console.log(products)
					var num = products[0].attributes.num //当前购物车里该商品的数量  
					var rid = products[0].id //该商品在购物车里的真实id
					console.log(rid)
					console.log(num)
					//购物车里已经添加过当前商品，只是修改数量
					var todo = AV.Object.createWithoutData('Cart', rid);
					todo.set('num', ++num);
					// 保存到云端
					todo.save();
					alert("success")
				}).catch(function(error) {
					console.log(JSON.stringify(error));
				});
			}
		}).catch(function(error) {
			console.log(JSON.stringify(error));
		});
	} else {
		location.href = "../login/login.html?RetUrl=" + getPageUrl()
	}
}